package plugin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import plugin.dao.UserCommonMapper;
import plugin.dao.UserMapper;
import pojo.User;
import tk.mybatis.mapper.entity.Config;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.mapperhelper.MapperHelper;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author Feuler
 * @since 2020/11/13
 **/

public class PluginTest {
    final InputStream inputStream = Resources.getResourceAsStream("plugin/sqlMapConfig.xml");
    final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    final SqlSession sqlSession = sqlSessionFactory.openSession(true);
    final UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    final MapperHelper mapperHelper = new MapperHelper();
    final UserCommonMapper userCommonMapper = sqlSession.getMapper(UserCommonMapper.class);

    public PluginTest() throws IOException {
        mapperHelper.setConfig(new Config());
        mapperHelper.registerMapper(UserCommonMapper.class);
        mapperHelper.processConfiguration(sqlSession.getConfiguration());
    }

    @After
    public void after() {
        sqlSession.close();
    }

    @Test
    public void test1() {
        try {
            userMapper.findUserById(1);
            System.out.println("test 1 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test2() {
        try {
            PageHelper.startPage(1, 1);
            List<User> users = userMapper.selectUser();
            users.forEach(System.out::println);
            PageInfo<User> userPageInfo = new PageInfo<>(users);
            System.out.println("总条数" + userPageInfo.getTotal());
            System.out.println("总页数" + userPageInfo.getPages());
            System.out.println("当前页" + userPageInfo.getPageNum());
            System.out.println("每页显示条数" + userPageInfo.getPageSize());
            System.out.println("test 2 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test3() {
        try {
            User user = new User();
            user.setId(1);
            System.out.println(userCommonMapper.selectOne(user));;
            System.out.println("test 3 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }

    @Test
    public void test4() {
        try {
            Example example = new Example(User.class);
            example.createCriteria().andEqualTo("id",1);
            userCommonMapper.selectByExample(example).forEach(System.out::println);
            System.out.println("test 4 passed");
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }
}
